package com.GameSpace.CoreAssets;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class StatisticsDBAdapter {

	  public static final String KEY_ID = "id";
	  public static final String KEY_PLAYER_NAME = "PlayerName";
	  public static final String KEY_TYPE = "Type";
	  public static final String KEY_LABEL = "Label";
	  public static final String KEY_VALUE = "Value";

	  private static final String DATABASE_NAME = "gamespacecore.db";
	  private static final String DATABASE_TABLE = "Statistics";
	  private static final int DATABASE_VERSION = 1;

	  private static final String DATABASE_CREATE =
	        "CREATE TABLE  IF NOT EXISTS " + DATABASE_TABLE + " (" 
	        + KEY_ID + " integer primary key autoincrement, "
	        + KEY_PLAYER_NAME + " text, "
	        + KEY_TYPE + " text, " 
	        + KEY_LABEL + " text, " 
	        + KEY_VALUE + " text);";
	  
	
	    private final Context context;
	    private DatabaseHelper DBHelper;
	    private SQLiteDatabase db;

	    public StatisticsDBAdapter(Context ctx) 
	    {
	        this.context = ctx;
	        DBHelper = new DatabaseHelper(context);
	    }
	     
	    private static class DatabaseHelper extends SQLiteOpenHelper 
	    {
	        DatabaseHelper(Context context) 
	        {
	            super(context, DATABASE_NAME, null, DATABASE_VERSION);
	         
	        }  	   
	            
	        @Override
	        public void onCreate(SQLiteDatabase db) 
	        {         	
	            db.execSQL(DATABASE_CREATE);
	        }

	        @Override
	        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
	                              int newVersion) 
	        {
	        	db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
	        	onCreate(db);
	        }
	    } 
	    
	    //opens the database
	    public StatisticsDBAdapter open() throws SQLException 
	    {
	        db = DBHelper.getWritableDatabase();
	        return this;
	    }
        //closes the database   
	    public void close() 
	    {
	        DBHelper.close();
	    }
	        
	    //insert an record into the database
	    public long insertStatistic(Statistic s) 
	    {	    
	        ContentValues initialValues = new ContentValues();
	        initialValues.put(KEY_PLAYER_NAME, s.getPlayerName());
	        initialValues.put(KEY_TYPE, s.getType());
	        initialValues.put(KEY_LABEL, s.getLabel());
	        initialValues.put(KEY_VALUE, s.getValue()); 
	        return db.insert(DATABASE_TABLE, null, initialValues);
	    }

	    //updates a particular record
	    public boolean updateStatistic(Statistic s) 
	    {
	    	ContentValues initialValues = new ContentValues();
	    	initialValues.put(KEY_PLAYER_NAME, s.getPlayerName());
	    	initialValues.put(KEY_TYPE, s.getType());
	        initialValues.put(KEY_LABEL, s.getLabel());
	        initialValues.put(KEY_VALUE, s.getValue()); 
	        return db.update(DATABASE_TABLE, initialValues, KEY_PLAYER_NAME + "=" +"'"+s.getPlayerName()+"'", null) > 0;
	    }
	    
	    //deletes a particular record
	    public boolean deleteStatistic(String s) 
	    {
	        return db.delete(DATABASE_TABLE, KEY_PLAYER_NAME + "=" +"'"+s+"'", null) > 0;
	    }

	    public Cursor getStatistic(String s) 
	    {
	    	String orderby = KEY_TYPE +" ASC";
	    	Cursor c = db.query(DATABASE_TABLE, new String[] {
					KEY_ID,
		     		KEY_TYPE,
		     		KEY_LABEL,
		     		KEY_VALUE}, 
		     		KEY_LABEL + "='" + s +"'", null, null, null, orderby);

	    	return c;
	    }
	    
	    //retrieves all records
	    public Cursor getAllStatistics() 
	    {
	    	String orderby = KEY_TYPE +" ASC";
	    	
	    	Cursor c = db.query(DATABASE_TABLE, new String[] {
	    						KEY_ID,
	    						KEY_PLAYER_NAME,
	        		     		KEY_TYPE,
	        		     		KEY_LABEL,
	        		     		KEY_VALUE}, 
	        		     		null, null, null, null, orderby);

	        return c;
	    }
}